package com.furnitur.modules.admin.controller.product;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furnitur.common.api.CommonResult;
import com.furnitur.core.entity.product.FurnitureCategory;
import com.furnitur.modules.admin.service.product.FurnitureCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/furniture/category")
@Validated
public class FurnitureCategoryController {

    @Autowired
    private FurnitureCategoryService categoryService;

    @GetMapping("/list")
    public CommonResult<Page<FurnitureCategory>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name) {
        log.info("Getting category list - page: {}, size: {}, name: {}", page, size, name);
        Page<FurnitureCategory> result = categoryService.getList(page, size, name);
        log.info("Found {} categories", result.getRecords().size());
        return CommonResult.success(result);
    }

    @PostMapping
    public CommonResult<FurnitureCategory> create(@RequestBody @Valid FurnitureCategory category) {
        return CommonResult.success(categoryService.create(category));
    }

    @PutMapping("/{id}")
    public CommonResult<FurnitureCategory> update(
            @PathVariable Long id,
            @RequestBody @Valid FurnitureCategory category) {
        category.setId(id);
        return CommonResult.success(categoryService.update(category));
    }

    @DeleteMapping("/{id}")
    public CommonResult<Void> delete(@PathVariable Long id) {
        categoryService.delete(id);
        return CommonResult.success(null);
    }

    @GetMapping("/all")
    public CommonResult<List<FurnitureCategory>> getAllCategories() {
        log.info("Received request for all categories");
        List<FurnitureCategory> categories = categoryService.getAllCategories();
        log.info("Returning {} categories", categories.size());
        return CommonResult.success(categories);
    }
} 